#coding:utf8
import random

# 棋盘上棋子信息，空0 玩家1 电脑2
board = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
]

# 玩家输入和棋盘位置的对应
ROW = {'1': 0, '2': 1, '3': 2}
COL = {'a': 0, 'b': 1, 'c': 2}

# 棋子显示，玩家o 电脑x
CHESS = [' ', 'o', 'x']

# 判断游戏是否结束
def isFinished():
    # 判断每一行
    if [1, 1, 1] in board:
        print 'You win!'
        return True
    if [2, 2, 2] in board:
        print 'AI wins!'
        return True
    # 判断每一列
    for i in range(3):
        if board[0][i] == board[1][i] == board[2][i] == 1:
            print 'You win!'
            return True
        if board[0][i] == board[1][i] == board[2][i] == 2:
            print 'AI wins!'
            return True
    # 判断对角线
    if (board[0][0] == board[1][1] == board[2][2] == 1) or (
        board[2][0] == board[1][1] == board[0][2] == 1):
        print 'You win!'
        return True
    if (board[0][0] == board[1][1] == board[2][2] == 2) or (
        board[2][0] == board[1][1] == board[0][2] == 2):
        print 'AI wins!'
        return True
    # 判断是否平局，如果还有空位0就还没有
    draw = True
    for i in range(3):
        if 0 in board[i]:
            draw = False
    if draw:
        print 'Draw game.'
        return True
    return False

'''
是否可走

2自己 1000
2对方 900

1自己0对手 100
1对方0自己 90

全空 10

中间 1
'''

# 对于一条线上的打分策略
def calcPoint(line):
    point = 0
    if line.count(2) == 2:  # 2个自己子
        point += 1000
    if line.count(1) == 2:  # 2个对方子
        point += 900
    if line.count(2) == 1 and line.count(1) == 0:  # 1个自己子，1个空位
        point += 100
    if line.count(2) == 0 and line.count(1) == 1:  # 1个对方子，1个空位
        point += 90
    if line.count(0) == 3:  # 全空位
        point += 10
    return point

# 电脑落子
def moveAI():
    print 'AI\'s turn...'
    # 初始化每个位置的得分
    point = [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
    ]
    max_point = -1  # 最高分
    position = (0, 0)  # 最高分位置
    for i in range(3):
        for j in range(3):
            if board[i][j] != 0:
                point[i][j] = -1
                continue
            # 所在行得分
            point[i][j] += calcPoint(board[i])
            # 所在列得分
            line = [board[k][j] for k in range(3)]
            point[i][j] += calcPoint(line)
            # 左上到右下得分
            if i == j:
                line = [board[k][k] for k in range(3)]
                point[i][j] += calcPoint(line)
            # 右上到左下得分
            if i + j == 2:
                line = [board[k][2 - k] for k in range(3)]
                point[i][j] += calcPoint(line)
            # 中间额外加分
            if i == 1:
                point[i][j] += 1
            if j == 1:
                point[i][j] += 1
            if point[i][j] > max_point:
                # 更新最高分
                max_point = point[i][j]
                position = (i, j)
#    print point
    board[position[0]][position[1]] = 2

# 玩家落子
def moveMan():
    print 'Your turn...'
    while True:
        try:
            # 获取并转换落子位置
            move = raw_input('choose a position (e.g. a1/c2/b3...):\n')
            pos_row = ROW[move[1]]
            pos_col = COL[move[0]]
            if board[pos_row][pos_col] == 0:
                board[pos_row][pos_col] = 1  # 记录
                return
        except:
            pass

# 显示棋盘，包括格子、位置标记和棋子
def showBoard():
    print '    a   b   c  '
    for i in range(3):
        print '  |---|---|---|'
        print i+1, '|',
        for j in range(3):
            print '%s |' % CHESS[board[i][j]],
        print
    print '  |---|---|---|'


if __name__ == '__main__':
    yourturn = True  # 玩家先手
    showBoard()  # 显示棋盘
    while not isFinished():
        # 判断是否结束，没结束则继续
        if yourturn:
            moveMan()  # 玩家落子
        else:
            moveAI()  # 电脑落子
        showBoard()
        yourturn = not yourturn;  # 换手

